home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  4.5 KB  |  234 lines  |  [TEXT/KAHL]

  1. /* Unix-specific code for Xconq.
  2.    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
  3.    Stanley T. Shebs.
  4.  
  5. Xconq is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.  See the file COPYING.  */
  9.  
  10. /* Unix interface stuff.  Do NOT attempt to use this file in a non-Unix
  11.    system, not even an ANSI one! */
  12.  
  13. #include "config.h"
  14. #include "misc.h"
  15. #include "dir.h"
  16. #include "lisp.h"
  17. #include "module.h"
  18.  
  19. #include <signal.h>
  20. #include <sys/time.h>
  21.  
  22. extern char *xconqlib;
  23.  
  24. #ifndef XCONQLIB
  25. #define XCONQLIB "../lib"
  26. #endif
  27.  
  28. char *
  29. default_library_filename()
  30. {
  31.     return XCONQLIB;
  32. }
  33.  
  34. char *
  35. news_filename()
  36. {
  37.     make_pathname(xconqlib, NEWSFILE, "", spbuf);
  38.     return spbuf;
  39. }
  40.  
  41. char *
  42. saved_game_filename()
  43. {
  44.     return "save.xconq";
  45. }
  46.  
  47. char *
  48. checkpoint_filename()
  49. {
  50.     return "check.xconq";
  51. }
  52.  
  53. char *
  54. error_save_filename()
  55. {
  56.     return "ack!.xconq";
  57. }
  58.  
  59. char *
  60. statistics_filename()
  61. {
  62.     return STATSFILE;
  63. }
  64.  
  65. /* Attempt to open a library file. */
  66.  
  67. FILE *
  68. open_module_library_file(module)
  69. Module *module;
  70. {
  71.     FILE *fp = NULL;
  72.  
  73.     /* Don't try to do on anon modules? */
  74.     if (module->name == NULL)
  75.       return NULL;
  76.     /* Generate library pathname. */
  77.     make_pathname(xconqlib, module->name, "g", spbuf);
  78.     /* Now try to open the file. */
  79.     fp = fopen(spbuf, "r");
  80.     if (fp != NULL) {
  81.     /* Remember the filename where we found it. */
  82.     module->filename = copy_string(spbuf);
  83.     }
  84.     return fp;
  85. }
  86.  
  87. FILE *
  88. open_module_explicit_file(module)
  89. Module *module;
  90. {
  91.     if (module->filename == NULL)
  92.       return NULL;
  93.     return (fopen(module->filename, "r"));
  94. }
  95.  
  96. FILE *
  97. open_library_file(filename)
  98. char *filename;
  99. {
  100.     char fullnamebuf[1024];
  101.     FILE *fp = NULL;
  102.  
  103.     fp = fopen(filename, "r");
  104.     if (fp == NULL) {
  105.     /* Generate library pathname. */
  106.     make_pathname(xconqlib, filename, NULL, fullnamebuf);
  107.     fp = fopen(fullnamebuf, "r");
  108.     }
  109.     return fp;
  110. }
  111.  
  112. void
  113. make_pathname(path, name, extn, pathbuf)
  114. char *path, *name, *extn, *pathbuf;
  115. {
  116.     strcpy(pathbuf, "");
  117.     if (!empty_string(path)) {
  118.     strcat(pathbuf, path);
  119.     strcat(pathbuf, "/");
  120.     }
  121.     strcat(pathbuf, name);
  122.     /* Don't add a second identical extension, but do add if extension
  123.        is different (in case we want "foo.12" -> "foo.12.g" for instance) */
  124.     if (strrchr(name, '.')
  125.     && extn
  126.     && strcmp((char *) strrchr(name, '.') + 1, extn) == 0)
  127.       return;
  128.     if (!empty_string(extn)) {
  129.     strcat(pathbuf, ".");
  130.     strcat(pathbuf, extn);
  131.     }
  132. }
  133.  
  134. /* Remove a saved game from the system. */
  135.  
  136. void
  137. remove_saved_game()
  138. {
  139.     unlink(saved_game_filename());
  140. }
  141.  
  142. /* Default behavior on explicit kill. */
  143.  
  144. void
  145. stop_handler(sig, code, scp, addr)
  146. int sig, code;
  147. struct sigcontext *scp;
  148. char *addr;     
  149. {
  150.     close_displays();
  151.     exit(1);
  152. }
  153.  
  154. /* This routine attempts to save the state before dying. */
  155.  
  156. void
  157. crash_handler(sig, code, scp, addr)
  158. int sig, code;
  159. struct sigcontext *scp;
  160. char *addr;     
  161. {
  162.     static int already_been_here = FALSE;
  163.  
  164.     if (!already_been_here) {
  165.     already_been_here = TRUE;
  166.     close_displays();  
  167.     printf("Fatal error encountered. Signal %d code %d\n", sig, code);
  168.     write_entire_game_state("ack!.xconq");
  169.     }
  170.     abort();
  171. }
  172.  
  173. /* Accidental disconnection saves state. */
  174.  
  175. void
  176. hup_handler(sig, code, scp, addr)
  177. int sig, code;
  178. struct sigcontext *scp;
  179. char *addr;     
  180. {
  181.     static int already_been_here = FALSE;
  182.  
  183.     if (!already_been_here) {
  184.     already_been_here = TRUE;
  185.     close_displays();
  186.     printf("Somebody was disconnected, saving the game.\n");
  187.     write_entire_game_state("ack!.xconq");
  188.     }
  189.     abort();
  190. }
  191.  
  192. void
  193. init_signal_handlers()
  194. {
  195.     signal(SIGINT, stop_handler);
  196.     if (0 /* don't accidently quit */ && !Debug) {
  197.     signal(SIGINT, SIG_IGN);
  198.     } else {
  199.     signal(SIGINT, SIG_DFL);
  200. /*    signal(SIGINT, crash_handler);  */
  201.     }
  202.     signal(SIGHUP, hup_handler);
  203.     signal(SIGSEGV, crash_handler);
  204.     signal(SIGFPE, crash_handler);
  205.     signal(SIGILL, crash_handler);
  206.     signal(SIGINT, crash_handler);
  207.     signal(SIGQUIT, crash_handler);
  208.     signal(SIGTERM, crash_handler);
  209.     /* The following signals may not be available everywhere. */
  210. #ifdef SIGBUS
  211.     signal(SIGBUS, crash_handler);
  212. #endif /* SIGBUS */
  213. #ifdef SIGSYS
  214.     signal(SIGSYS, crash_handler);
  215. #endif /* SIGSYS */
  216. }
  217.  
  218. struct timeval reallasttime = { 0, 0 };
  219.  
  220. struct timeval realcurtime;
  221.  
  222. int
  223. n_seconds_elapsed(n)
  224. int n;
  225. {
  226.     gettimeofday(&realcurtime, NULL);
  227.     if (realcurtime.tv_sec > (reallasttime.tv_sec + (n - 1))) {
  228.     reallasttime = realcurtime;
  229.     return TRUE;
  230.     } else {
  231.     return FALSE;
  232.     }
  233. }
  234.